<?php

require_once '../include/common.inc.php';
if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}
//?????
if($continue!="")header("localtion: c_promoter.php"); 

$menu_index = 2;
$page_title = 'TABLA';
$guide_message = 'Customer | Promoter Information';
$page_url = 'c_promoter.php?';
$ppp = 20;

getMyParam('action');
getMyParam('search_key');

if($action!=""){
    getMyParam('password');
    getMyParam('status');
	getMyParam('prestatus');
    getMyParam('company'); 
    getMyParam('firstname');
    getMyParam('lastname'); 
	getMyParam('website');

    getMyParam('street');
    getMyParam('city');
	getMyParam('country');
    getMyParam('state');
	getMyParam('zip');
	
	getMyParam('street2');
	getMyParam('city2');
	getMyParam('state2');
	getMyParam('zip2');	
	getMyParam('country2');	
	
	getMyParam('email');
    getMyParam('phone1');
	getMyParam('phone');
	getMyParam('mobile1');
	getMyParam('mobile');
	getMyParam('fax1');
	getMyParam('fax');
	
	getMyParam('b_bank');
	getMyParam('b_registration');
	getMyParam('b_account');
	getMyParam('b_iban');
	getMyParam('b_swift');

	getMyParam('rank');
			
	getMyParam('faq1');
	getMyParam('faq2');
	getMyParam('faq3');
	getMyParam('faq4');
	getMyParam('faq5');
	getMyParam('faq6');
	getMyParam('faq7');
	getMyParam('faq8');
	

						
	getMyParam('cid');	
	//var_dump($_POST);
	if($faq1=='1')
	{
		$faq1='1';
		$faq2='0';
	}
	if($faq1!='1')
	{
		$faq1='0';
		$faq2='1'	;
	}	
	$faq3 = isset($_POST['faq3']) && $_POST['faq3'] == 1 ? 1 : 0;
    $faq4 = isset($_POST['faq4']) && $_POST['faq4'] == 1 ? 1 : 0;
    $faq5 = isset($_POST['faq5']) && $_POST['faq5'] == 1 ? 1 : 0;
    $faq6 = isset($_POST['faq6']) && $_POST['faq6'] == 1 ? 1 : 0;
    $faq7 = isset($_POST['faq7']) && $_POST['faq7'] == 1 ? 1 : 0;
    $faq8 = isset($_POST['faq8']) && $_POST['faq8'] == 1 ? 1 : 0;

if ($action=="add") {
		
		$sql="insert into `promoter_info` SET `password1` = '".md5($password)."',
			`password2` = '".$password."',
			`email` = '".$email."',
			`company` = '".$company."',
			`firstname` = '".$firstname."',
			`lastname` = '".$lastname."',
			`website` = '".$website."',
			`street` = '".$street."',
			`city` = '".$city."',
			`state` = '".$state."',
			`zip` = '".$zip."',
			`country` = '".$country."',
			`street2` = '".$street2."',
			`city2` = '".$city2."',
			`state2` = '".$state2."',
			`zip2` = '".$zip2."',
			`country2` = '".$country2."',
			`phone` = '".$phone1."||".$phone."',
			`mobile` = '".$mobile1."||".$mobile."',
			`fax` = '".$fax1."||".$fax."',
			`b_bank` = '".$b_bank."',
			`b_registration` = '".$b_registration."',
			`b_account` = '".$b_account."',
			`b_iban` = '".$b_iban."',
			`b_swift` = '".$b_swift."',
			`faq1` = '".$faq1."',
			`faq2` = '".$faq2."',
			`faq3` = '".$faq3."',
			`faq4` = '".$faq4."',
			`faq5` = '".$faq5."',
			`faq6` = '".$faq6."',
			`faq7` = '".$faq7."',
			`faq8` = '".$faq8."',
			`rank` = '".$rank."',
			`status` = '".$status."',
			`date` = ".time()."";

		$db->query($sql);
		
	    $msg="This Promoter has been added.";
		$href="c_promoter.php";
		include_once('menu_list.php');
		require_once PrintEot('a_header');
		require_once PrintEot('a_msg');
		require_once PrintEot('a_footer');
		exit;
    }

if ($action =="edit") {

		$sql="update `promoter_info` SET `password1` = '".md5($password)."',
			`password2` = '".$password."',
			`email` = '".$email."',
			`company` = '".$company."',
			`firstname` = '".$firstname."',
			`lastname` = '".$lastname."',
			`website` = '".$website."',
			`street` = '".$street."',
			`city` = '".$city."',
			`state` = '".$state."',
			`zip` = '".$zip."',
			`country` = '".$country."',
			`street2` = '".$street2."',
			`city2` = '".$city2."',
			`state2` = '".$state2."',
			`zip2` = '".$zip2."',
			`country2` = '".$country2."',
			`phone` = '".$phone1."||".$phone."',
			`mobile` = '".$mobile1."||".$mobile."',
			`fax` = '".$fax1."||".$fax."',
			`b_bank` = '".$b_bank."',
			`b_registration` = '".$b_registration."',
			`b_account` = '".$b_account."',
			`b_iban` = '".$b_iban."',
			`b_swift` = '".$b_swift."',
			`faq1` = '".$faq1."',
			`faq2` = '".$faq2."',
			`faq3` = '".$faq3."',
			`faq4` = '".$faq4."',
			`faq5` = '".$faq5."',
			`faq6` = '".$faq6."',
			`faq7` = '".$faq7."',
			`faq8` = '".$faq8."',
			`rank` = '".$rank."',
			`status` = '".$status."' where id=".$cid;
        $db->query($sql);
		
		if($status!=$prestatus)
		{
			$sql_promoter="select firstname,lastname,password2,email,rank from promoter_info where id=".$cid;
			$ary_promoter=$mysql->fetch($sql_promoter);
			
			$firstname=$ary_promoter[0]["firstname"];
			$lastname=$ary_promoter[0]["lastname"];
			$username=$firstname." ".$lastname;
			$password=$ary_promoter[0]["password2"];
			$email=$ary_promoter[0]["email"];
			$rank=$RANK_ARY[$ary_promoter[0]["rank"]];
			$promoterid=$cid;
			if($status==1)
			{
				include_once('../mail/approval_promoter.php');
				sendhtmlmail($from_name,$from_email,$to_name,$to_email,$subject,$content);
			}
			if($status==2)
			{
				include_once('../mail/disapproval_promoter.php');
				sendhtmlmail($from_name,$from_email,$to_name,$to_email,$subject,$content);
			}
			//echo $from_name."<br/>".$from_email."<br/>".$to_name."<br/>".$to_email."<br/>".$subject."<br/>".$content;
		}		
		$msg="Successfully updated promoter info.";
		$href="c_promoter.php";
		include_once('menu_list.php');
		require_once PrintEot('a_header');
		require_once PrintEot('a_msg');
		require_once PrintEot('a_footer');
		exit;

} 
if ($action == "del") {
    getMyParam('cid');
    if (is_numeric($cid)) {
        $sql = "DELETE FROM `promoter_info` WHERE `id` = '" . $cid . "'";
        $db->query($sql);
		$msg="This promoter has been deleted. ";
		$href="c_promoter.php";
		include_once('menu_list.php');
		require_once PrintEot('a_header');
		require_once PrintEot('a_msg');
		require_once PrintEot('a_footer');
		exit;
    }
}

}

$sql_where = '';
if ($action == "search" && trim($search_key)  != "") {
    $sql_where = "where `id` LIKE '%$search_key%'  OR `firstname` LIKE '%$search_key%'  OR `lastname` LIKE '%$search_key%' OR `email` LIKE '%$search_key%' ";
}




$sql = "SELECT `id` FROM `promoter_info` " . $sql_where;
$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);

// *** page code start ***
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}
// *** page code end ***

$sql=" select p.*,(select case when sum(pc.commission)>0 then sum(pc.commission) else 0 end from promoter_commission pc , cus_order co  where  pc.order_id = co.id and co.pay_flag=0 and co.deliver_status=1 and pc.promoter_id=p.id) as totalcms, (select case when sum(pc.commission)>0 then sum(pc.commission) else 0 end from promoter_commission pc , cus_order co where  pc.order_id = co.id and co.pay_flag=0 and co.deliver_status = 1 and pc.status=0 and pc.promoter_id=p.id) as totalcms_notpaid , (select sum(cost)-sum(pay_amount) from promoter_order where promoter_id=p.id and pay_flag=0 and deliver_status=1) as consume from promoter_info p ".$sql_where. " ORDER BY p.id DESC LIMIT $start_limit, $ppp";
///SQL?????(????????????)
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    if ($action == "search" && trim($search_key) != "") {
        $rs['id'] = str_replace($search_key, "<font color=red>$search_key</font>", $rs['id']);
        $rs['email'] = str_replace($search_key, "<font color=red>$search_key</font>", $rs['email']);
		$rs['firstname'] = str_replace($search_key, "<font color=red>$search_key</font>", $rs['firstname']);
        $rs['lastname'] = str_replace($search_key, "<font color=red>$search_key</font>", $rs['lastname']);
    }
    $out_ary[] = $rs;

}

for($i=0;$i<count($out_ary);$i++)
{
	$out_ary[$i]['date']=date('Y-m-d',$out_ary[$i]['date']);
	if($out_ary[$i]['status']==0)$out_ary[$i]['status']="Non-Approved";
	if($out_ary[$i]['status']==1)$out_ary[$i]['status']="Approved";
	if($out_ary[$i]['status']==2)$out_ary[$i]['status']="Disapproved";
	
	$tmpCmsNotPaid=$out_ary[$i]['totalcms_notpaid']-$out_ary[$i]['consume'];
	$tmpCmsNotPaid = $tmpCmsNotPaid > 0 ? $tmpCmsNotPaid : 0;
	$out_ary[$i]['totalcms_notpaid']=number_format($tmpCmsNotPaid,2,".","");
}


$page_show = getPageStr($sql_all_num, $page, $ppp, $page_url);

include_once('menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_c_promoter');
require_once PrintEot('a_footer');

?>